package com.zb.Service.impl;

import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import com.zb.Service.DepartmentService;
import com.zb.entity.Department;
import com.zb.mapper.DepartmentMapper;
@Component("DepartmentService")
public class DepartmentServiceImpl implements DepartmentService {
	@Autowired
	DepartmentMapper dm;
	//取得根节点
	public List<Department> getTree() {
		List<Department> dlist = dm.showRootNode();
		for (Department dept : dlist) {
			dept.setIconCls("icon-package");
			findChild(dept);
		}
		return dlist;
	}
	//获取根节点的子节点
	public void findChild(Department dept) {
		List<Department> dlist = dm.findChild(dept.getId());
		for (Department childdept : dlist) {
			findChild(childdept);
			dept.addChild(childdept);
		}
	}
	
	public List<Department> getTree2() {
		List<Department> dlist = dm.showRootNode();
		for (Department dept : dlist) {
			findChild2(dept);
		}
		return dlist;
	}
	public void findChild2(Department dept) {
		List<Department> dlist = dm.findChild(dept.getId());
		for (Department childdept : dlist) {
			childdept.setState("closed");
			findChild2(childdept);
			dept.addChild(childdept);
		}
	}
	
	
	public void addRootNode(Department dept) {
		dm.addRootNode(dept);
	}
	public void addChildNode(Department dapt) {
		dm.addChildNode(dapt);
	}
	public void updateRootNode(Department dept) {
		dm.updateRootNode(dept);
	}
	public void updateChildNode(Department dept) {
		dm.updateChildNode(dept);
	}
	public void deleteNode(int id) {
		dm.deleteNode(id);
	}
	public Department getDeptById(int id) {
		return dm.getDeptById(id);
	}
	public int hasChild(int id) {
		return dm.hasChild(id);
	}
}
